package com.bmf.module.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.constant.AdminResultEnum;
import com.bmf.module.rbac.entity.RbacMenu;
import com.bmf.module.rbac.entity.RbacMenuRole;
import com.bmf.module.rbac.entity.RbacRole;
import com.bmf.module.rbac.entity.RbacUserRole;
import com.bmf.module.rbac.mapper.RbacMenuRoleMapper;
import com.bmf.module.rbac.service.RbacMenuRoleService;
import com.bmf.module.rbac.service.RbacMenuService;
import com.bmf.module.rbac.service.RbacUserRoleService;
import com.bmf.module.rbac.vo.RbacMenuResultVo;
import com.bmf.module.rbac.vo.RbacMenuRoleCreateVo;
import com.bmf.module.rbac.vo.RbacMenuRoleDeleteVo;
import com.bmf.module.rbac.vo.RbacMenuRoleUpdateVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (RbacMenuRole)表服务实现类
 *
 * @author makejava
 * @since 2022-12-27 15:33:17
 */
@Service("rbacMenuRoleService")
public class RbacMenuRoleServiceImpl extends ServiceImpl<RbacMenuRoleMapper, RbacMenuRole> implements RbacMenuRoleService {

    @Autowired
    private RbacMenuRoleService rbacMenuRoleService;

    @Autowired
    private RbacMenuService rbacMenuService;

    @Override
    public List<RbacMenuResultVo> selectByRoleId(Integer roleid) {
        LambdaQueryWrapper<RbacMenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacMenuRole::getRoleid, roleid);
        List<RbacMenuRole> list = rbacMenuRoleService.list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        //menuid
        List<Integer> ids = list.stream().map(RbacMenuRole::getMenuid).collect(Collectors.toList());
        List<RbacMenu> rbacMenus = rbacMenuService.listByIds(ids);
        if(CollUtil.isEmpty(rbacMenus)){
            return null;
        }
        List<RbacMenuResultVo> rbacMenuResultVos = rbacMenuService.arrangementRbacMenu(rbacMenus);
        return rbacMenuResultVos;
    }


    @Override
    public List<Integer> selectAll(Integer roleid) {
        LambdaQueryWrapper<RbacMenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacMenuRole::getRoleid, roleid);
        List<RbacMenuRole> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.stream().map(RbacMenuRole::getMenuid).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(RbacMenuRoleUpdateVo rbacMenuRoleUpdateVo) {
        //删除全部的
        LambdaQueryWrapper<RbacMenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacMenuRole::getRoleid, rbacMenuRoleUpdateVo.getRoleId());
        remove(wrapper);
        //重新添加
        List<RbacMenuRoleCreateVo> rbacMenuRoleCreateVos = rbacMenuRoleUpdateVo.getRbacMenuRoleCreateVos();
        if(CollUtil.isNotEmpty(rbacMenuRoleCreateVos)){
            List<RbacMenuRole> rbacMenuRoles = BeanCopyUtils.copyBeanList(rbacMenuRoleCreateVos, RbacMenuRole.class);
            saveBatch(rbacMenuRoles);
        }
    }
}
